package com.hb.common.handler.backup;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.ecm.backup.constant.DbCommand;
import com.sl.ecm.backup.dto.Database;
import com.sl.ecm.backup.enums.DbEnum;
import com.sl.ecm.backup.exception.DbBackupException;
import com.sl.ecm.backup.factory.DbBackupFactory;
import com.sl.ecm.backup.handler.os.OSHandler;
import com.sl.ecm.backup.util.PathPermissions;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;

@Component
@Lazy(false)
public class DMBackupHandler extends AbstractDbBackupHandler {
	@Resource
	protected PathPermissions pathPermissions;

	@Override
	protected void backupAfter(String dbBackupPath) {
		String logPath = getParentDirectory(dbBackupPath) + "\\" + removeFileExtension(dbBackupPath) + ".log";
		FileUtil.del(logPath);
	}

	@Override
	protected void restoreAfter(String dbProgramPath) {
		backupAfter(dbProgramPath);
	}

	@Override
	protected String[] createBackupCommand(Database database, String dbFilePath) throws DbBackupException {
		String command = StrUtil.format(DbCommand.DMS_BACKUP,
			database.getUsername(),
			database.getPassword(),
			database.getHost(),
			database.getPort(),
			database.getDbName(),
			getFileName(dbFilePath),
			removeFileExtension(getFileName(dbFilePath))+".log",
			getParentDirectory(dbFilePath));
		// 组装命令
		OSHandler osHandler = osUtil.getOSHandler();
		return osHandler.assembleCommand(command);
	}

	public static String getParentDirectory(String filePath) {
		File file = new File(filePath);
		File parentDir = file.getParentFile();
		if (parentDir != null) {
			return parentDir.getAbsolutePath();
		} else {
			return null; // 如果文件没有父目录，返回 null
		}
	}

	public static String removeFileExtension(String filePath) {
		File file = new File(filePath);
		String fileName = file.getName();
		int lastIndexOfDot = fileName.lastIndexOf('.');
		if (lastIndexOfDot > 0) {
			return fileName.substring(0, lastIndexOfDot);
		}
		return fileName; // 如果没有后缀，返回原文件名
	}

	@Override
	protected String[] createRestoreCommand(Database database, String dbFilePath) throws DbBackupException {
		String command = StrUtil.format(DbCommand.DMS_RESTORE,
			database.getUsername(),
			database.getPassword(),
			database.getHost(),
			database.getPort(),
			database.getDbName(),
			getFileName(dbFilePath),
			removeFileExtension(getFileName(dbFilePath))+".log",
			getParentDirectory(dbFilePath)
		);
		// 组装命令
		OSHandler osHandler = osUtil.getOSHandler();
		return osHandler.assembleCommand(command);
	}

	@Override
	protected String dbProgramPath() {
		OSHandler osHandler = osUtil.getOSHandler();
		return pathPermissions.getDbProgramPath() + osHandler.getOsPath() + DbEnum.DMS.name().toLowerCase() + "/bin";
	}

	public static String getFileName(String filePath) {
		File file = new File(filePath);
		return file.getName();
	}

	@Override
	protected String dbFileTye() {
		return "dmp";
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		DbBackupFactory.register(DbEnum.DMS, this);
	}
}
